package org.emailclassifier.preprocessing;

import java.util.Comparator;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeMap;

public class Freq {

	
	/**
	 * Returns the word frequency list of the given text
	 * @param text The text to get the word frequency list of
	 * @return The word frequencies
	 */
	public static Map<String, Integer> getWordFrequencies( String text, boolean stoplist ) {
		
		String[] stopWords;
		if (stoplist) {
			stopWords = "a,able,about,across,after,all,almost,also,am,among,an,and,any,are,as,at,be,because,been,but,by,can,cannot,could,dear,did,do,does,either,else,ever,every,enron,for,from,get,got,had,has,have,he,her,hers,him,his,how,however,i,if,in,into,is,it,its,just,least,let,like,likely,may,me,might,most,must,my,neither,no,nor,not,of,off,often,on,only,or,other,our,own,rather,said,say,says,she,should,since,so,some,than,that,the,their,them,then,there,these,they,this,tis,to,too,twas,us,wants,was,we,were,what,when,where,which,while,who,whom,why,will,with,would,yet,you,your".split(",");
		} else {
			stopWords = new String[0];
		}
		
		Set<String> stopWordsHash = new HashSet<String>();
		for( String stopWord : stopWords ) {
			stopWordsHash.add(stopWord);
		}
		
		Map<String, Integer> wordFrequencies = new TreeMap<String, Integer>();
	    String content = text.replaceAll("[!,:;=&*%@<>'\"\\.\\?\\[\\]\\(\\)]", " ");
        StringTokenizer tokenizedEmail = new StringTokenizer( content );
	    
        while ( tokenizedEmail.hasMoreTokens() ) {
			String word = tokenizedEmail.nextElement().toString().toLowerCase();
			if( !(stopWordsHash.contains(word)  || word.length() < 2 ) ) {
				Integer count = wordFrequencies.get(word);
				if (count == null) {
					count = 1;
				}
				else {
					count += 1;
				}
				wordFrequencies.put(word, count);
			}
		}
        return wordFrequencies;		
	}
	
	/**
	 * Returns the sorted word frequency list of the given text
	 * @param text The text to get the word frequency list of
	 * @return The word frequencies (sorted in descending order)
	 */
	public static TreeMap<String, Integer> getSortedWordFrequencies( String text, boolean stoplist ) {
		
		Map<String, Integer> freq = getWordFrequencies(text, stoplist);
		FreqListComparator cmp = new FreqListComparator(freq);
		@SuppressWarnings("unchecked")
		TreeMap<String, Integer> sorted_freq = new TreeMap<String, Integer>(cmp);
		
		sorted_freq.putAll(freq);
		return sorted_freq;
	}
}

@SuppressWarnings("rawtypes")
class FreqListComparator implements Comparator {

	Map base;
	public FreqListComparator(Map base) {
		this.base = base;
	}
	
	public int compare(Object a, Object b) {

	    if((Integer)base.get(a) < (Integer)base.get(b)) {
	    	return 1;
	    } else if((Integer)base.get(a) == (Integer)base.get(b)) {
	    	return 0;
	    } else {
	    	return -1;
	    }
	}
}
